# -*- coding: utf-8 -*-
'''
@文件    :merchant.py
@说明    :合利宝商户进件相关接口
@时间    :2025/08/02 14:22:48
@作者    :浪晋
@版本    :1.0
'''


from . import base
from .. import utils


class MerchantClient(base.BaseClient):
    def register(
            self, orderNo, signName, showName, creditCode, regionCode, address, email, 
            legalPersonName, legalPersonID, legalPersonPhone, 
            accountName, accountNo, bankCode, settleBankType,
            callbackUrl
    ):
        """第一步、注册子商户"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'firstClassMerchantNo': self.merchant_number,
            "orderNo": orderNo,
            "signName": signName, # 子商户公司名称
            "showName": showName, # 子商户简称
            "merchantType": "ENTERPRISE",
            "legalPerson": legalPersonName, # 法人
            "legalPersonID": legalPersonID, # 法人
            "idType":"IDCARD", # 法人
            "orgNum": creditCode,  # 营业执照号
            "businessLicense": creditCode, # 营业执照号
            "regionCode": regionCode,  # 区县编码
            "industryTypeCode":"332", # 行业类型编码
            "address": address,
            "linkman": legalPersonName,  # 联系人
            "linkPhone": legalPersonPhone, # 联系人
            "linkmanType":"LEGAL",   # 联系人类型 LEGAL/SUPER
            "email": email,
            "bankCode": bankCode,  # 结算卡联行号
            "accountName": accountName, # 结算账户
            "accountNo": accountNo, # 结算账号
            "settleBankType": settleBankType, # 结算卡类型  TOPUBLIC/TOPRIVATE
            "settlementPeriod":"T1", # 结算类型  T1/D1/D0
            "settlementMode":"NOTOPEN", # 结算方式  NOTOPEN/AUTO/SELF
            "settleMode":"MERGE", # 结算模式 MERCHANT/MERGE
            "merchantCategory":"EDUCATION_TRAINING", # 经营类别
            "authorizationFlag": "true", # 授权使用平台商秘钥
            "needPosFunction": "false", # 是否需要开通 POS 功能
            "agreeProtocol": "true", # 是否同意协议
            "needAuthorize": "true",  # 如果要开通微信支付则为true
            "callbackUrl": callbackUrl
        }
        data = self._build_merchant_data('register', body)
        return self._request(url, headers=headers, data=data)
    

    def registerQuery(self, orderNo):
        """商户进件查询接口"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        data = {
            'firstClassMerchantNo': self.merchant_number,
            "orderNo": orderNo
        }
        params = self._build_merchant_data('registerQuery', data)
        return self._request(url, headers=headers, data=data)


    def merchantInfoQuery(self, merchantNo):
        """商户信息查询"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'firstClassMerchantNo': self.merchant_number,
            "merchantNo": merchantNo
        }
        data = self._build_merchant_data('merchantInfoQuery', body)
        return self._request(url, headers=headers, data=data)


    def uploadImageUrl(self, merchantNo, credentialType, credentialUrl):
        """子商户资质url上传接口"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'credentialType': credentialType,
            'credentialUrl': credentialUrl,
            "merchantNo": merchantNo
        }
        data = self._build_merchant_data('uploadImageUrl', body)
        return self._request(url, headers=headers, data=data)
    

    def open_quickpay_product(self, merchantNo, calcType, onlineCardType, value, minFee):
        """开通快捷支付产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'productType': "QUICKPAY",
            'firstClassMerchantNo': self.merchant_number,
            "merchantNo":merchantNo,
            "calcType": calcType, # 计算类型  RATIO/SINGLE
            "onlineCardType": onlineCardType, # 卡类型  DEBIT/CREDIT
            "value": value, # 费率
            "minFee": minFee # 最低手续费
        }
        data = self._build_merchant_data('openProduct', body)
        return self._request(url, headers=headers, data=data)


    def query_quickpay_product(self, merchantNo):
        """查询快捷支付产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'productType': "QUICKPAY",
            'firstClassMerchantNo': self.merchant_number,
            "merchantNo":merchantNo,
            "calcType": "RATIO", # 计算类型  RATIO/SINGLE
            "onlineCardType": "DEBIT", # 卡类型  DEBIT/CREDIT
        }
        data = self._build_merchant_data('productQuery', body)
        return self._request(url, headers=headers, data=data)


    def open_apppay_product(self, merchantNo, appPayType, payType, feePurpose, value, minFee):
        """开通扫码产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'productType': "APPPAY",
            'firstClassMerchantNo': self.merchant_number,
            "merchantNo":merchantNo,
            "appPayType": appPayType, # 客户端类型
            "payType": payType,  # 支付类型
            "feePurpose": feePurpose # 费率类型
        }
        if feePurpose == "DEFAULT":
            body["appFeeMode"] = "DEFAULT"  # 默认模式,不分段 
            body["value"] = value # 费率
            body["minFee"] = minFee # 最低手续费
        else:
            body["appFeeMode"] = "RANGE" # 分段模式, 采用分段方式。
            body["feeRanges"] = [
                {"start":0 ,"end":5000,"fee":value,"minFee":minFee,"maxFee":200,"calcType":"RATIO","onlineCardType":"DEBIT"}
            ] 
        data = self._build_merchant_data('openProduct', body)
        return self._request(url, headers=headers, data=data)


    def query_apply_product(self, merchantNo, appPayType, payType, feePurpose):
        """查询扫码支付产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'productType': "APPPAY",
            "merchantNo":merchantNo,
            "appPayType": appPayType, # 客户端类型
            "payType": payType,  # 支付类型
            "feePurpose": feePurpose # 费率类型
        }
        if feePurpose != "DEFAULT":
            body["appFeeMode"] = "RANGE"
        data = self._build_merchant_data('productQuery', body)
        return self._request(url, headers=headers, data=data)


    def open_wxpay_product(self, merchantNo, operateType, merchantChannelName, appIds, authPayDirs):
        """微信公众号报备"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            "merchantNo":merchantNo,
            "subscribeAppIds": appIds,
            "appIds": appIds,
            "authPayDirs": authPayDirs,
            "merchantChannelName": merchantChannelName,
            "orderNo":  utils.parameter.generate_order_no("apply"),
            "operateType": operateType,  # MODIFY/REPEAT
            "reportId": utils.parameter.generate_order_no("apply")  # 116042685
        }
        data = self._build_merchant_data('wxPublicApply', body)
        return self._request(url, headers=headers, data=data)


    def query_wxpay_product(self, merchantNo):
        """查询开通微信支付产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            "merchantNo":merchantNo,
            "orderNo": utils.parameter.generate_order_no("query")
        }
        data = self._build_merchant_data('wxPublicApplyQuery', body)
        return self._request(url, headers=headers, data=data)


    def get_sign_url(self, merchantNo, callBackUrl):
        """获取电子合同签约url"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            "merchantNo":self.merchant_number,
            "emerchantNo": merchantNo,
            "callBackUrl": callBackUrl
        }
        data = self._build_merchant_data('getSignContractUrl', body)
        return self._request(url, headers=headers, data=data)


    def open_settlement_product(self, merchantNo, calcType, settlementPeriod, settleBankType, value, floating, minFee):
        """开通结算产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'productType': "SETTLEMENT",
            'firstClassMerchantNo': self.merchant_number,
            "merchantNo":merchantNo,
            "calcType": calcType, # 计算类型  RATIO/SINGLE
            "settlementPeriod": settlementPeriod,  # 资金周期 T1/DO
            "settleBankType": settleBankType, # 账户属性
            "value": value, # 费率
            "floating": floating, # 浮动值
            "minFee": minFee # 最低手续费
        }
        data = self._build_merchant_data('openProduct', body)
        return self._request(url, headers=headers, data=data)
    

    def query_settlement_product(self, merchantNo, calcType, settlementPeriod, settleBankType):
        """查询结算产品"""
        url = self.base_url + "/trx/merchantEntry/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            'productType': "SETTLEMENT",
            'firstClassMerchantNo': self.merchant_number,
            "merchantNo":merchantNo,
            "calcType": calcType, # 计算类型  RATIO/SINGLE
            "settlementPeriod": settlementPeriod,  # 资金周期 T1/DO
            "settleBankType": settleBankType # 账户属性
        }
        data = self._build_merchant_data('productQuery', body)
        return self._request(url, headers=headers, data=data)